//
//  FileManager.m
//  Pitch
//
//  Created by zhujinhui on 14-9-7.
//  Copyright (c) 2014年 zhujinhui. All rights reserved.
//

#import "FileManager.h"

@implementation FileManager



+(NSString *)documentDir{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [paths objectAtIndex:0];
//    NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
    return documents;
}


+(NSString *)homeDir{
    NSString *homeDir = NSHomeDirectory();
    return homeDir;
}

+(NSString *)resourceWithFileName:(NSString *)name
                             type:(NSString *)type{
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
    return imagePath;

}

+(NSString *)generateFilePath:(NSString *) fileName toDirectory:(DirectoryType) type{
    NSString *filePath = nil;
    switch (type) {
        case DirectoryTypeDocument:{
            NSString *documentDir = [FileManager documentDir];
            filePath = [documentDir stringByAppendingPathComponent:fileName];
        }
            break;
            
        default:
            break;
    }
    return filePath;
}


+(BOOL)writeFile:(NSString *)fileName toDirectory:(DirectoryType) type withData:(NSData *)data{
    BOOL result = NO;
    NSString *path = [FileManager generateFilePath:fileName toDirectory:type];
//    path = [NSString stringWithFormat:@"%@.jpg",path];
    if (path) {
        result = [data writeToFile:fileName atomically:YES];
        NSError *error = nil;
        result = [data writeToFile:fileName options:NSDataWritingWithoutOverwriting error:&error];
        if (error) {
            NSLog(@"error:%@",error);
        }
    }
    
    return result;
}


@end
